package Joe.SiteManager.Web.Action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import Joe.HotSite.BLL.BannerProvider;
import Joe.HotSite.BLL.HotContentProvider;
import Joe.HotSite.BLL.HotPictureProvider;
import Joe.HotSite.BLL.Domain.*;
import Joe.SiteManager.Web.Common.PageBase;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;

public class BannerAction extends PageBase{
	public Integer bid = 0;
	public Integer getBid() {
		return bid;
	}

	public void setBid(Integer bid) {
		this.bid = bid;
	}

	public Banner banner = null;
	public Banner getBanner() {
		return banner;
	}

	public void setBanner(Banner banner) {
		this.banner = banner;
	}
	
	public Map bannerMap = null;
	
	public Map getBannerMap() {
		return bannerMap;
	}

	public void setBannerMap(Map bannerMap) {
		this.bannerMap = bannerMap;
	}

	private String imgBannerFileName;
	
	public String getImgBannerFileName() {
		return imgBannerFileName;
	}

	public void setImgBannerFileName(String imgBannerFileName) {
		this.imgBannerFileName = imgBannerFileName;
	}

	private String imgBannerContentType;

	public String getImgBannerContentType() {
		return imgBannerContentType;
	}

	public void setImgBannerContentType(String imgBannerContentType) {
		this.imgBannerContentType = imgBannerContentType;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	List<Banner> bannerList = null;
	public List<Banner> getBannerList() {
		return bannerList;
	}

	public void setBannerList(List<Banner> bannerList) {
		this.bannerList = bannerList;
	}

	public String list() throws Exception {
		bannerList = BannerProvider.getInstance().getList();
		return SUCCESS;
	}
	
	public String edit() throws Exception {
		if(request().getMethod() == "POST"){
			Hotpicture hp = new Hotpicture();
			hp.setPictureIndex(1);
			hp.setTitle("");
			hp.setType(1);
			hp.setUri(upload());
			//Integer pid = HotPictureProvider.getInstance().add(hp);
			banner.setCreateTime(new Timestamp(System.currentTimeMillis()));
			banner.setHotpicture(hp);
			BannerProvider.getInstance().edit(banner);
			return SUCCESS;
		}
		return "display";
	}
	
	private File imgBanner;
	public File getImgBanner() {
		return imgBanner;
	}

	public void setImgBanner(File imgBanner) {
		this.imgBanner = imgBanner;
	}

	public String upload() throws IOException{
		if(imgBanner != null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSS");
			Random r = new Random();
			String fileName = sdf.format(new Date()) + r.nextInt(100) + "." + imgBannerContentType.substring(imgBannerContentType.indexOf("/") + 1);
			String targetPath = this.context().getRealPath("/images");
			File file = new File(targetPath,fileName);
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			FileUtils.copyFile(imgBanner, file);
			return fileName;
		}
		return null;
	}
	
	public void prepareEdit(){
		Integer bid = Integer.valueOf(request().getParameter("bid"));
		if(bid > 0){
			bannerMap = BannerProvider.getInstance().getDeepByBid(bid);
		}
	}
	
}
